import { Repository } from 'typeorm';

interface IPageConfig {
  select: string[];
  where: any[] | string;
  order: {};
}
export class BaseService {
  /**
   * 分页
   * @param model
   * @param config
   * @param page
   * @param size
   * @returns
   */
  async pagination<T extends Repository<any>>(
    model: T,
    config: IPageConfig = {
      select: [],
      where: [],
      order: {
        id: 'DESC',
      },
    },
    page: number = 1,
    size: number = 10
  ) {
    const rows = await model.find({
      select: config.select,
      where: config.where,
      order: config.order,
      skip: (page - 1) * 10,
      take: size,
    });

    const total = await model.count({
      where: config.where,
    });

    return {
      pagination: {
        total: total,
        totalPage: Math.ceil(total / size),
      },
      rows: rows,
    };
  }
}
